package com.charlie.meetingmanagement.service.impl;

import com.charlie.meetingmanagement.mapper.MeetingMapper;
import com.charlie.meetingmanagement.pojo.po.Meeting;
import com.charlie.meetingmanagement.pojo.po.PageBean;
import com.charlie.meetingmanagement.pojo.po.Result;
import com.charlie.meetingmanagement.service.MeetingService;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MeetingServiceImpl implements MeetingService {

    private static final Logger log = LoggerFactory.getLogger(MeetingServiceImpl.class);
    @Resource
    private MeetingMapper meetingMapper;

    @Override
    public List<Meeting> meetingInfo() {
        return meetingMapper.meetingInfo();
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, Integer meetingroomId, Integer meetingroomSize, String meetingroomHeadname) {
        Long count = meetingMapper.count(meetingroomId,meetingroomSize,meetingroomHeadname);
//        这里使用pageHelper进行分页，非常好用
        PageHelper.startPage(page, pageSize);
        List<Meeting> meetingList = meetingMapper.page(meetingroomId,meetingroomSize,meetingroomHeadname);

        return new PageBean(count, meetingList);
    }

    @Override
    public Result insertMeeting(Meeting meeting) {
        try {
            meetingMapper.insertMeeting(meeting);
        }catch (Exception e){
            log.info(e.getMessage());
            return Result.error("会议室房间号已存在");
        }
        return Result.success();
    }

    @Override
    public Result delete(Integer meetingroomId) {
        try {
            meetingMapper.delete(meetingroomId);
        }catch (Exception e){
            log.info(e.getMessage());
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    @Override
    public Result updateMeeting(Meeting meeting) {
        try {
            meetingMapper.update(meeting);
        }catch (Exception e){
            log.info(e.getMessage());
            return Result.error(e.getMessage());
        }
        return Result.success();
    }
}
